﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VisionCore
{
    public class EQHelper : ProcedureHelper_EQ
    {
        Thread thread_Alarm;
        bool isRun = true;
        public List<bool> blnFs { get; set; } = new List<bool>();
        public List<bool> blnWs { get; set; } = new List<bool>();

        public List<bool> blnFs_Buf { get; set; } = new List<bool>();
        public List<bool> blnWs_Buf { get; set; } = new List<bool>();

        private int alarmNum = 256;
        private int warningNum = 512;

        Status_EQStatus status = new Status_EQStatus();

        public int AlarmNum
        {
            get { return alarmNum; }
        }

        public int WarningNum
        {
            get { return warningNum; }
        }

        public void Start()
        {
            AlarmInit();

            thread_Alarm = new Thread(ThreadFunc_Alarm);
            thread_Alarm.IsBackground = true;
            thread_Alarm.Start();

            DelayOnHelper_EQ.Start();
        }

        public void Quit()
        {
            isRun = false;
            DelayOnHelper_EQ.Quit();
        }

        private void AlarmInit()
        {
            blnFs.Clear();
            blnWs.Clear();
            for (int i = 0; i < AlarmNum; i++)
            {
                blnFs.Add(false);
                blnFs_Buf.Add(false);
            }

            for (int i = 0; i < WarningNum; i++)
            {
                blnWs.Add(false);
                blnWs_Buf.Add(false);
            }
        }

        int timecot = 0;
        private void ThreadFunc_Alarm()
        {
            while (isRun)
            {
                status.Main();

                if (Kernel.motion.IsRun)
                {
                    FreshAlarm();
                    FreshWarning();
                }

                if (timecot > 500)
                {
                    Alarm = GetAlarmValid() > 0;
                    Warning = GetWarningValid() > 0;
                    timecot = 0;
                }
                timecot++;
                SystemTimeCounter.timeBeginPeriod(1);
                Thread.Sleep(1);
                SystemTimeCounter.timeEndPeriod(1);
            }
        }

        private void FreshAlarm()
        {
            blnFs[0] = Alarms.PowerOFF;      
            //blnFs[1] = Alarms.CDAError;
            //blnFs[2] = Alarms.VACError;
            //blnFs[3] = Alarms.EfemCDAError;
            //blnFs[4] = Alarms.EfemVACError;


            blnFs[10] = Alarms.Door1_Open;
            blnFs[11] = Alarms.Door2_Open;
            //blnFs[12] = Alarms.Door3_Open;
            //blnFs[13] = Alarms.Door4_Open;
            //blnFs[14] = Alarms.Door5_Open;
            blnFs[16] = Alarms.Door5_NotRest;

            blnFs[17] = Alarms.EMO1_Error;
            blnFs[18] = Alarms.EMO2_Error;

            blnFs[20] = Alarms.CV1_Y_LMT_P;
            blnFs[21] = Alarms.CV1_Y_LMT_N;
            blnFs[22] = Alarms.CV1_Y_IsHomeAlready;
            blnFs[23] = Alarms.CV1_Y_Alarm;
            blnFs[24] = Alarms.CV1_Y_ErrorStop;

            blnFs[25] = Alarms.CV2_Y_LMT_P;
            blnFs[26] = Alarms.CV2_Y_LMT_N;
            blnFs[27] = Alarms.CV2_Y_IsHomeAlready;
            blnFs[28] = Alarms.CV2_Y_Alarm;
            blnFs[29] = Alarms.CV2_Y_ErrorStop;

            blnFs[30] = Alarms.CV1_TrackSW_Y_LMT_P;
            blnFs[31] = Alarms.CV1_TrackSW_Y_LMT_N;
            blnFs[32] = Alarms.CV1_TrackSW_Y_IsHomeAlready;
            blnFs[33] = Alarms.CV1_TrackSW_Y_Alarm;
            blnFs[34] = Alarms.CV1_TrackSW_Y_ErrorStop;

            blnFs[35] = Alarms.CV2_TrackSW_Y_LMT_P;
            blnFs[36] = Alarms.CV2_TrackSW_Y_LMT_N;
            blnFs[37] = Alarms.CV2_TrackSW_Y_IsHomeAlready;
            blnFs[38] = Alarms.CV2_TrackSW_Y_Alarm;
            blnFs[39] = Alarms.CV2_TrackSW_Y_ErrorStop;

            blnFs[40] = Alarms.CCD1_X_LMT_P;
            blnFs[41] = Alarms.CCD1_X_LMT_N;
            blnFs[42] = Alarms.CCD1_X_IsHomeAlready;
            blnFs[43] = Alarms.CCD1_X_Alarm;
            blnFs[44] = Alarms.CCD1_X_ErrorStop;

            blnFs[45] = Alarms.CCD1_Y_LMT_P;
            blnFs[46] = Alarms.CCD1_Y_LMT_N;
            blnFs[47] = Alarms.CCD1_Y_IsHomeAlready;
            blnFs[48] = Alarms.CCD1_Y_Alarm;
            blnFs[49] = Alarms.CCD1_Y_ErrorStop;

            blnFs[50] = Alarms.CCD1_Z_LMT_P;
            blnFs[51] = Alarms.CCD1_Z_LMT_N;
            blnFs[52] = Alarms.CCD1_Z_IsHomeAlready;
            blnFs[53] = Alarms.CCD1_Z_Alarm;
            blnFs[54] = Alarms.CCD1_Z_ErrorStop;

            blnFs[55] = Alarms.CCD2_X_LMT_P;
            blnFs[56] = Alarms.CCD2_X_LMT_N;
            blnFs[57] = Alarms.CCD2_X_IsHomeAlready;
            blnFs[58] = Alarms.CCD2_X_Alarm;
            blnFs[59] = Alarms.CCD2_X_ErrorStop;

            blnFs[60] = Alarms.CCD2_Y_LMT_P;
            blnFs[61] = Alarms.CCD2_Y_LMT_N;
            blnFs[62] = Alarms.CCD2_Y_IsHomeAlready;
            blnFs[63] = Alarms.CCD2_Y_Alarm;
            blnFs[64] = Alarms.CCD2_Y_ErrorStop;

            blnFs[65] = false;
            blnFs[66] = false;
            blnFs[67] = false;
            blnFs[68] = false;

            blnFs[70] = Alarms.CCD2_Z_LMT_P;
            blnFs[71] = Alarms.CCD2_Z_LMT_N;
            blnFs[72] = Alarms.CCD2_Z_IsHomeAlready;
            blnFs[73] = Alarms.CCD2_Z_Alarm;
            blnFs[74] = Alarms.CCD2_Z_ErrorStop;

            blnFs[75] = Alarms.Insp1_Y_LMT_P;
            blnFs[76] = Alarms.Insp1_Y_LMT_N;
            blnFs[77] = Alarms.Insp1_Y_IsHomeAlready;
            blnFs[78] = Alarms.Insp1_Y_Alarm;
            blnFs[79] = Alarms.Insp1_Y_ErrorStop;

            blnFs[80] = false;
            blnFs[81] = false;
            blnFs[82] = false;
            blnFs[83] = false;
            blnFs[84] = false;

            blnFs[85] = Alarms.Insp2_Y_LMT_P;
            blnFs[86] = Alarms.Insp2_Y_LMT_N;
            blnFs[87] = Alarms.Insp2_Y_IsHomeAlready;
            blnFs[88] = Alarms.Insp2_Y_Alarm;
            blnFs[89] = Alarms.Insp2_Y_ErrorStop;

            blnFs[90] = false;
            blnFs[91] = false;
            blnFs[92] = false;
            blnFs[93] = false;
            blnFs[94] = false;

            blnFs[95] = Alarms.Insp3_Y_LMT_P;
            blnFs[96] = Alarms.Insp3_Y_LMT_N;
            blnFs[97] = Alarms.Insp3_Y_IsHomeAlready;
            blnFs[98] = Alarms.Insp3_Y_Alarm;
            blnFs[99] = Alarms.Insp3_Y_ErrorStop;

            blnFs[100] = false;
            blnFs[101] = false;
            blnFs[102] = false;
            blnFs[103] = false;
            blnFs[104] = false;

            blnFs[105] = Alarms.Insp4_Y_LMT_P;
            blnFs[106] = Alarms.Insp4_Y_LMT_N;
            blnFs[107] = Alarms.Insp4_Y_IsHomeAlready;
            blnFs[108] = Alarms.Insp4_Y_Alarm;
            blnFs[109] = Alarms.Insp4_Y_ErrorStop;

            blnFs[110] = false;
            blnFs[111] = false;
            blnFs[112] = false;
            blnFs[113] = false;
            blnFs[114] = false;

            blnFs[115] = Alarms.Enter_CV1_ErrorStop;
            blnFs[116] = Alarms.Enter_CV2_ErrorStop;
            blnFs[117] = Alarms.Insp1_CV1_ErrorStop;
            blnFs[118] = Alarms.Insp1_CV2_ErrorStop;
            blnFs[119] = Alarms.Insp2_CV1_ErrorStop;
            blnFs[120] = Alarms.Insp2_CV2_ErrorStop;
            blnFs[121] = Alarms.Insp3_CV1_ErrorStop;
            blnFs[122] = Alarms.Insp3_CV2_ErrorStop;
            blnFs[123] = Alarms.Insp4_CV1_ErrorStop;
            blnFs[124] = Alarms.Insp4_CV2_ErrorStop;     
         

            if (!ManualMode)
            {

            }

          
        }

        private void FreshWarning()
        {
            blnWs[0] = RTCHelper.IOEvent.Alive_RemoteExist.AliveBeatTimeOut;
            blnWs[1] = !RTCHelper.IOEvent.Alive_RemoteExist.AliveBeatTimeOut&&!VisionAutoMode&&AutoMode;

            blnWs[5] = Warnings.CurParamSelError;

            if (ManualMode)
            {

            }

            blnWs[20] = Warnings.AutoFocusFail;
            blnWs[21] = Warnings.FindMarkFail;
            blnWs[22] = Warnings.CalcHomMatFail;
            blnWs[23] = Warnings.CodeReader1Fail;
            blnWs[24] = Warnings.LoadMapFail;
            blnWs[25] = Warnings.WaitAVITimeOut;
            blnWs[26] = Warnings.LightChangeError;
            blnWs[27] = Warnings.WaitAVICompTimeOut;
            blnWs[28] = Warnings.WaferIDNotSame;

            blnWs[30] = Warnings.Insp1_PresenceError;
            blnWs[31] = Warnings.Insp2_PresenceError;
            blnWs[32] = Warnings.Insp3_PresenceError;
            blnWs[33] = Warnings.Insp4_PresenceError;

            blnWs[36] = Warnings.Insp1_VacError;
            blnWs[37] = Warnings.Insp2_VacError;
            blnWs[38] = Warnings.Insp3_VacError;
            blnWs[39] = Warnings.Insp4_VacError;

           
            blnWs[42] = Warnings.CV1_PinUpError;
            blnWs[43] = Warnings.CV1_PinDownError;
            blnWs[44] = Warnings.CV2_PinUpError;
            blnWs[45] = Warnings.CV2_PinDownError;
     
            blnWs[50] = Warnings.Insp1_PinUpError;
            blnWs[51] = Warnings.Insp1_PinDownError;
            blnWs[52] = Warnings.Insp2_PinUpError;
            blnWs[53] = Warnings.Insp2_PinDownError;
            blnWs[54] = Warnings.Insp3_PinUpError;
            blnWs[55] = Warnings.Insp3_PinDownError;
            blnWs[56] = Warnings.Insp4_PinUpError;
            blnWs[57] = Warnings.Insp4_PinDownError;


            blnWs[60] = Warnings.CV1_Y_AtWaitError;
            blnWs[61] = Warnings.CV1_Y_AtWorkError;
            blnWs[62] = Warnings.CV2_Y_AtWaitError;
            blnWs[63] = Warnings.CV2_Y_AtWorkError;
            blnWs[64] = Warnings.CV1_TrackSW_Y_AtLDPt1Error;
            blnWs[65] = Warnings.CV1_TrackSW_Y_AtLDPt2Error;
            blnWs[66] = Warnings.CV1_TrackSW_Y_AtInsp1Error;
            blnWs[67] = Warnings.CV1_TrackSW_Y_AtInsp2Error;
            blnWs[68] = Warnings.CV2_TrackSW_Y_AtLDPt1Error;
            blnWs[69] = Warnings.CV2_TrackSW_Y_AtLDPt2Error;
            blnWs[70] = Warnings.CV2_TrackSW_Y_AtInsp1Error;
            blnWs[71] = Warnings.CV2_TrackSW_Y_AtInsp2Error;

            blnWs[75] = Warnings.CCD1_X_AtCH1MarkError;
            blnWs[76] = Warnings.CCD1_X_AtCH1WorkError;
            blnWs[77] = Warnings.CCD1_X_AtCH2MarkError;
            blnWs[78] = Warnings.CCD1_X_AtCH2WorkError;

            blnWs[80] = Warnings.CCD1_Y_AtCH1MarkError;
            blnWs[81] = Warnings.CCD1_Y_AtCH1WorkError;
            blnWs[82] = Warnings.CCD1_Y_AtCH2MarkError;
            blnWs[83] = Warnings.CCD1_Y_AtCH2WorkError;
      
            blnWs[85] = Warnings.CCD1_Z_AtCH1MarkError;
            blnWs[86] = Warnings.CCD1_Z_AtCH1WorkError;
            blnWs[87] = Warnings.CCD1_Z_AtCH2MarkError;
            blnWs[88] = Warnings.CCD1_Z_AtCH2WorkError;

            blnWs[90] = Warnings.CCD2_X_AtCH1MarkError;
            blnWs[91] = Warnings.CCD2_X_AtCH1WorkError;
            blnWs[92] = Warnings.CCD2_X_AtCH2MarkError;
            blnWs[93] = Warnings.CCD2_X_AtCH2WorkError;

            blnWs[100] = Warnings.CCD2_Y_AtCH1MarkError;
            blnWs[101] = Warnings.CCD2_Y_AtCH1WorkError;
            blnWs[102] = Warnings.CCD2_Y_AtCH2MarkError;
            blnWs[103] = Warnings.CCD2_Y_AtCH2WorkError;

            blnWs[105] = Warnings.CCD2_Z_AtCH1MarkError;
            blnWs[107] = Warnings.CCD2_Z_AtCH1WorkError;
            blnWs[108] = Warnings.CCD2_Z_AtCH2MarkError;
            blnWs[109] = Warnings.CCD2_Z_AtCH2WorkError;

            blnWs[110] = Warnings.Insp1_Y_AtWorkError;
            blnWs[111] = Warnings.Insp1_Y_AtWaitError;
            blnWs[112] = Warnings.Insp2_Y_AtWorkError;
            blnWs[113] = Warnings.Insp2_Y_AtWaitError;
            blnWs[114] = Warnings.Insp3_Y_AtWorkError;
            blnWs[115] = Warnings.Insp3_Y_AtWaitError;
            blnWs[116] = Warnings.Insp4_Y_AtWorkError;
            blnWs[117] = Warnings.Insp4_Y_AtWaitError;
           
            blnWs[120] = Warnings.Insp1_RM_Z_AtWorkError;
            blnWs[121] = Warnings.Insp1_RM_Z_AtWaitError;
            blnWs[122] = Warnings.Insp2_RM_Z_AtWorkError;
            blnWs[123] = Warnings.Insp2_RM_Z_AtWaitError;
            blnWs[124] = Warnings.Insp3_RM_Z_AtWorkError;
            blnWs[125] = Warnings.Insp3_RM_Z_AtWaitError;
            blnWs[126] = Warnings.Insp4_RM_Z_AtWorkError;
            blnWs[127] = Warnings.Insp4_RM_Z_AtWaitError;


          
        }

        private void AlarmReset()
        {
            for (int i = 0; i < AlarmNum; i++)
            {
                blnFs[i] = false;
            }

            for (int i = 0; i < WarningNum; i++)
            {
                blnWs[i] = false;
            }
        }

        private int GetAlarmValid()
        {
            int num = 0;
            for (int i = 0; i < AlarmNum; i++)
            {
                if (blnFs[i])
                {
                    num++;
                }
            }
            return num;
        }

        private int GetWarningValid()
        {
            int num = 0;
            for (int i = 0; i < WarningNum; i++)
            {
                if (blnWs[i])
                {
                    num++;
                }
            }

            return num;
        }
    }
}
